import pdb

import pyautogui as pyg
import pydirectinput as pyd
import pygetwindow as gw
import keyboard
import time 
from Tools import Tools
# 打包方法 pyinstaller.exe --onefile 自动开盒.py
pause=False
shopping_pause=False
DNF_WD = gw.getWindowsWithTitle("Dungeon & Fighter")[0]
INIT_WIDTH=800
INIT_HEIGHT=600
def compute_position(x_offset,y_offset):
    y= DNF_WD.top+(INIT_HEIGHT/2+y_offset)*(DNF_WD.height/INIT_HEIGHT)
    x= DNF_WD.left+(INIT_WIDTH/2+x_offset)*(DNF_WD.width/INIT_WIDTH)
    return (x,y)

def click_dnf(x,y,duration=0.5):
    pyg.moveTo(x,y,duration=duration)
    pyg.mouseDown(button="left")
    pyg.mouseUp(button="left")

def pause():
    global pause 
    pause =  True
    print("暂停开盒子")
def start():
    global pause 
    pause =  False
    print("开始开盒子")
    openbox()
def openbox():

    print(DNF_WD,INIT_WIDTH,INIT_HEIGHT)
    tool = Tools(DNF_WD, INIT_WIDTH, INIT_HEIGHT)
    count=0
    while True:
        if pause:
            time.sleep(0.5)
        else:
            print(f"开始开盒子：{count}")
            pyd.press("4")
            #点击确认位置
            x,y = tool.compute_position(-30,65)
            #print(x,y)
            click_dnf(x,y,duration=0.1)
            time.sleep(0.2)
            x, y = tool.compute_position(0, 90)
            click_dnf(x, y,duration=0.1)
            #print(x, y)
            time.sleep(0.2)


def buy_pause():
    global shopping_pause
    shopping_pause =  True
    print("暂停买无色")

def buy_wuse():
    print("购买无色")
    global shopping_pause
    shopping_pause = False
    tool = Tools(DNF_WD, INIT_WIDTH, INIT_HEIGHT)
    num_str_5 = "5"
    num_str_1 = "1"
    for a in range(12):
        if not shopping_pause:
            x, y = tool.compute_abs_position(265,240)
            tool.shopping(x,y,num=num_str_5,zero_num=4)
    if not shopping_pause:
        x, y = tool.compute_abs_position(239, 333)
        tool.shopping(x, y, num=num_str_1, zero_num=4)
    if not shopping_pause:
        x, y = tool.compute_abs_position(269, 333)
        tool.shopping(x, y, num=num_str_1, zero_num=4)
    if not shopping_pause:
        x, y = tool.compute_abs_position(299, 333)
        tool.shopping(x, y, num=num_str_1, zero_num=4)
    if not shopping_pause:
        x, y = tool.compute_abs_position(329, 333)
        tool.shopping(x, y, num=num_str_1, zero_num=4)
    if not shopping_pause:
        x, y = tool.compute_abs_position(359, 333)
        tool.shopping(x, y, num=num_str_1, zero_num=4)

    if not shopping_pause:
        x, y = tool.compute_abs_position(239, 358)
        tool.shopping(x, y, num=num_str_1, zero_num=2)
    if not shopping_pause:
        x, y = tool.compute_abs_position(265, 358)
        tool.shopping(x, y, num=num_str_1, zero_num=2)
    if not shopping_pause:
        x, y = tool.compute_abs_position(299, 358)
        tool.shopping(x, y, num=num_str_1, zero_num=2)
    if not shopping_pause:
        x, y = tool.compute_abs_position(329, 358)
        tool.shopping(x, y, num=num_str_1, zero_num=2)
    if not shopping_pause:
        x, y = tool.compute_abs_position(359, 358)
        tool.shopping(x, y, num=num_str_1, zero_num=2)
    pause = True
sk_pause=False
def buy_sk_pause():
    global sk_pause
    sk_pause =  True
    print("暂停买混乱时空")
def buy_shikong():
    tool = Tools(DNF_WD, INIT_WIDTH, INIT_HEIGHT)
    #485 294
    global  sk_pause
    sk_pause = False
    for i in range(13):
        if sk_pause:
           time.sleep(2)
        else:
            time.sleep(1)
            x, y = tool.compute_abs_position(245,462)
            pyd.mouseDown(x,y)
            pyd.mouseUp(x,y)
            x, y = tool.compute_abs_position(486,294)
            pyd.mouseDown(x,y)
            pyd.mouseUp(x,y)
            pyd.press("2")
            for i in range(0,3): pyd.press("0")
            pyd.mouseDown(x, y)
            pyd.mouseUp(x, y)
            pyd.mouseDown(x, y)
            pyd.mouseUp(x, y)
            pyd.mouseDown(x,y,button="right")
            pyd.mouseUp(x, y, button="right")
            x, y = tool.compute_abs_position(235,245)
            tool.shopping(x, y, "4",1)
    sk_pause = True

keyboard.add_hotkey("ctrl+num_8",start)
keyboard.add_hotkey("ctrl+num_9",pause)
keyboard.add_hotkey("ctrl+num_6",buy_wuse)
keyboard.add_hotkey("ctrl+num_7",buy_pause)
keyboard.add_hotkey("ctrl+num_4",buy_shikong)
keyboard.add_hotkey("ctrl+num_5",buy_sk_pause)
print("1.按ctrl+num_8开始和ctrl+num_9结束,ctrl+num_6开始买无色， ctrl+num7暂停买无色,ctrl+num_4开始买混乱时空石， ctrl+num5暂停买无色混乱时空石 ")
keyboard.wait("esc")

        
